package com.ebarlas.fractal.compile;

import java.util.Hashtable;
import java.util.Map;

public class ByteClassLoader extends ClassLoader {
    
    private static final ByteClassLoader INSTANCE = new ByteClassLoader(ClassLoader.getSystemClassLoader());
    
    private ByteClassLoader(ClassLoader parent) {
        super(parent);
    }
    
    public static ByteClassLoader getInstance() {
        return INSTANCE;
    }
    
    private static Map<String, byte[]> classMap = new Hashtable<String, byte[]>();
    
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] bytes = classMap.get(name);
        
        if(bytes == null) {
            throw new ClassNotFoundException(name);
        }
        
        return defineClass(name, bytes, 0, bytes.length);
    }
    
    public static void registerClass(String name, byte[] bytes) {
        classMap.put(name, bytes);
    }
    
    public static void unregisterClass(String name) {
        classMap.remove(name);
    }    
    
}
